﻿Imports UCS_HardwareIO

Public Class Form1
    Private Decoder As New UCS_HardwareIO.Decoder
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim Ports As String() = IO.Ports.SerialPort.GetPortNames
        Dim Port As String
        For Each Port In Ports
            ComboBox1.Items.Add(Port)
        Next Port
        Timer1.Enabled = True
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If PortIO.IsOpen Then PortIO.Close()
        PortIO.PortName = ComboBox1.SelectedItem
        PortIO.Open()
        PortIO.DiscardInBuffer()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If PortIO.IsOpen Then
            'Led1.State = UCBase.Led.LedState.Led_On
            Label1.BackColor = Color.GreenYellow
            Label1.Text = "Open"
        Else
            'Led1.State = UCBase.Led.LedState.Led_Off
            Label1.BackColor = Color.Red
            Label1.Text = "Closed"
        End If
        If PortIO.IsOpen Then
            If CheckBox1.Checked Then
                If PortIO.BytesToRead > 0 Then
                    Dim F As String
                    F = PortIO.ReadLine
                    If F > "" Then
                        ListBox1.Items.Add(F)
                        CheckBox1.Checked = False
                        If F.Contains("Addr=[") Then
                            Dim T As String = F.Substring(F.IndexOf("[") + 1, F.IndexOf("]") - F.IndexOf("[") - 1)
                            Dim A() As String = T.Split(",")
                            TextBox1.Text = A(0)
                            TextBox2.Text = A(1)
                        End If
                    End If
                End If
            Else
                If PortIO.BytesToRead > 0 Then
                    Dim B(PortIO.BytesToRead) As Byte
                    PortIO.Read(B, 0, B.Length)
                    ListBox1.Items.Add(B)
                End If
            End If
        End If
    End Sub
    Public Function GetName() As Boolean
        Dim m_Name As String
        If MyBase.DesignMode = False Then
            Dim sc As Long = My.Computer.Clock.TickCount
            Do
                If My.Computer.Clock.TickCount > sc + 5000 Then
                    MsgBox("Get Name Error")
                    Exit Function
                End If
                Windows.Forms.Application.DoEvents()
            Loop Until PortIO.IsOpen
            If PortIO.IsOpen = True Then
                Dim BBuff As New UCS_HardwareIO.ByteBuffer
                Dim BC As Integer = 0
                PortIO.DiscardInBuffer()
                PortIO.DiscardOutBuffer()
                Dim B(2) As Byte
                B(0) = CByte(TextBox1.Text)
                B(1) = CByte(TextBox2.Text)
                B(2) = 0
                PortIO.Write(B, 0, 3)
                Dim S As Long = My.Computer.Clock.TickCount
                Debug.Print("Write Command")
                Do
                    If PortIO.BytesToRead >= 3 Then Exit Do
                Loop Until My.Computer.Clock.TickCount >= S + 5000
                Debug.Print("Buffer Filled:" & PortIO.BytesToRead)
                If PortIO.BytesToRead > 0 Then
                    Dim D(2) As Byte
                    PortIO.Read(D, 0, D.Length)
                    BBuff.Add(D)
                    Debug.Print(ByteToString(D))
                    BC = D(2)
                End If
                Do
                    If PortIO.BytesToRead >= BC Then Exit Do
                Loop Until My.Computer.Clock.TickCount >= S + 500
                Debug.Print("Buffer Filled:" & PortIO.BytesToRead)
                If PortIO.BytesToRead > 0 Then
                    Dim D(PortIO.BytesToRead) As Byte
                    PortIO.Read(D, 0, PortIO.BytesToRead)
                    BBuff.Add(D)
                    Debug.Print("Data Length : " & BBuff.Length)
                    Decoder.Decode(BBuff.ToArray)
                    m_Name = Decoder.DataToString
                    Debug.Print("Name=" & m_Name)
                    ListBox1.Items.Add("Name=" & m_Name)
                    Return True
                End If
                Return False
            End If
            Return False
        End If
        Return False
    End Function
    Private Function ByteToString(ByVal Bytes() As Byte) As String
        Dim S As String = "Byte Array = {"
        For Each B As Byte In Bytes
            S = S & B.ToString & ","
        Next
        S = S.Substring(0, S.Length - 1)
        S = S & "}"
        Return S
    End Function
    Private Sub Write(ByVal A As Integer, ByVal B As Integer, ByVal C As Integer)
        Dim Byt(2) As Byte
        Byt(0) = CByte(A)
        Byt(1) = CByte(B)
        Byt(2) = CByte(C)
        PortIO.Write(Byt, 0, 3)
    End Sub
    Public Function WriteB(ByVal Data() As Byte) As Boolean
        If PortIO.IsOpen = True Then
            'PortIO.ClearBuffers()
            Dim B(2 + Data.Length) As Byte
            'B(0) = CByte(m_AddrA)
            'B(1) = CByte(m_AddrB)
            B(2) = Data.Length
            Array.Copy(Data, 0, B, 3, Data.Length)
            'PortIO.Write(B)
            Dim S As Long = My.Computer.Clock.TickCount
            Do
                'If PortIO.BytesToRead >= 5 Then Exit Do
            Loop Until My.Computer.Clock.TickCount >= S + 500

            If PortIO.BytesToRead > 0 Then
                'Dim D As Byte()
                'D = PortIO.Read
                'Decoder.Decode(D)
                'If Decoder.DataToString = "OK" Then
                'm_Connected = True
                Return True
            End If
        End If
        'PortIO.ClosePort()
        Return False
        ' End If
        Return False
    End Function
    Public Function Read(ByVal Data() As Byte) As Byte()
        'PortIO.OpenPort()
        Dim sc As Long = My.Computer.Clock.TickCount
        Do
            If My.Computer.Clock.TickCount > sc + 5000 Then
                MsgBox("Get Name Error")
                Return Nothing
            End If
            Windows.Forms.Application.DoEvents()
        Loop Until PortIO.IsOpen
        If PortIO.IsOpen = True Then
            Dim BBuff As New ByteBuffer
            Dim BC As Integer = 0
            '  If m_AddrA + m_AddrB <= 0 Then Return Nothing
            ' PortIO.ClearBuffers()
            Dim B(2 + Data.Length) As Byte
            'B(0) = CByte(m_AddrA)
            'B(1) = CByte(m_AddrB)
            B(2) = Data.Length
            Array.Copy(Data, 0, B, 3, Data.Length)
            'PortIO.Write(B)
            Dim S As Long = My.Computer.Clock.TickCount
            Do
                If PortIO.BytesToRead >= 3 Then Exit Do
            Loop Until My.Computer.Clock.TickCount >= S + 5000
            If PortIO.BytesToRead > 0 Then
                Dim D(2) As Byte
                'D = PortIO.Read(3)
                BBuff.Add(D)
                Debug.Print(ByteToString(D))
                BC = D(2)
            End If
            Do
                If PortIO.BytesToRead >= BC Then Exit Do
            Loop Until My.Computer.Clock.TickCount >= S + 500
            If PortIO.BytesToRead > 0 Then
                'Dim D As Byte()
                'D = PortIO.Read
                'BBuff.Add(D)
                'PortIO.ClosePort()
                Return BBuff.ToArray
            End If
            'PortIO.ClosePort()
            Return Nothing
        End If
        Return Nothing
    End Function
    Public Function ReadText(ByVal Data() As Byte) As String
        'Decoder.Decode(Read(Data))
        'Return Decoder.DataToString
        Return ""
    End Function
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If PortIO.IsOpen Then PortIO.Close()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ListBox1.Items.Clear()

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        If PortIO.IsOpen Then
            CheckBox1.Checked = True
            PortIO.DiscardInBuffer()
            Write(1, 1, 0)
        Else
            MsgBox("No Open Port!", MsgBoxStyle.Critical)
        End If

    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        If PortIO.IsOpen Then
            Write(1, 2, CInt(TextBox1.Text))
            Write(1, 3, CInt(TextBox2.Text))
        Else
            MsgBox("No Open Port!", MsgBoxStyle.Critical)
        End If


    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        'Write(CInt(TextBox1.Text), CInt(TextBox2.Text), 0)
        GetName()
    End Sub
End Class
